//
//  KFDDetailedDiagramViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/3/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDDetailedDiagramViewController.h"
#import "ui_config.h"
#import "KFDCurrentPriceBoard.h"
#import "KFDHighLowPriceBoard.h"
#import "KFDCloseOpenPriceBoard.h"
#import "JTOptionBar.h"
#import "ui_config.h"
#import "KFDDetailedDiagramViewModel.h"
#import "NSDate+format.h"
#import "KFDDetailedDiagramViewModel.h"
#import "KFDDiagramPoint.h"
#import "BBStockChart.h"
#import "KFDAlarmSettingsViewController.h"
#import "KFDAccountManager.h"
#import "UIAlertController+KFD.h"
#import "KFDMAPriceLabel.h"
#import "KFDBOLLPriceLabel.h"
#import "KFDMACDPriceLabel.h"
#import "KFDKDJPriceLabel.h"

#define Float(a) (((NSNumber*)a).floatValue)

@interface KFDDetailedDiagramViewController ()<AxisXLabelProvider,UIGestureRecognizerDelegate>

@property (strong,nonatomic) UIScrollView* diagramContainer;
@property (strong, nonatomic) JTOptionBar* topDiagramOptionBar;
@property (strong,nonatomic) BBChartView* topDiagramView;
@property (strong,nonatomic) KFDMAPriceLabel* maPriceLabel;
@property (strong,nonatomic) KFDBOLLPriceLabel* bollPriceLabel;
@property (strong,nonatomic) JTOptionBar* bottomDiagramOptionBar;
@property (strong,nonatomic) BBChartView* bottomDiagramView;
@property (strong,nonatomic) KFDMACDPriceLabel* macdPriceLabel;
@property (strong,nonatomic) KFDKDJPriceLabel* kdjPriceLabel;


@property (strong, nonatomic) KFDCurrentPriceBoard *currentPriceBoard;
@property (strong, nonatomic) KFDHighLowPriceBoard *highLowPriceBoard;
@property (strong, nonatomic) KFDCloseOpenPriceBoard *closeOpenPriceBoard;
@property (strong, nonatomic) JTOptionBar* timeTypeOptionBar;

@property (strong, nonatomic) UILabel* titleLabel;

@end

@implementation KFDDetailedDiagramViewController
{
    CGFloat _naviBarHeight;
    CGFloat _boardHeight;
    CGFloat _barHeight;
    CGFloat _topDiagramOptionBarHeight;
    CGFloat _topDiagramViewHeight;
    CGFloat _bottomDiagramOptionBarHeight;
    CGFloat _bottomDiagramViewHeight;
}

-(instancetype)init
{
    if(self=[super init]){
        self.viewModel=[[KFDDetailedDiagramViewModel alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header-alarm-ico"] style:UIBarButtonItemStylePlain target:self action:@selector(alarmButtonPressed)];
    [self setupTitleView];
    [self defineHeights];
    [self setupPriceBoards];
    [self setupTimeTypeOptionBar];
    
    [self setupDiagramTheme];
    [self setupDiagramContainer];
    [self setupTopDiagramOptionBar];
    [self setupTopDiagramView];
    [self setupTopPriceLabel];
    [self setupBottomDiagramOptionBar];
    [self setupBottomDiagramView];
    [self setupBottomPriceLabel];
    
    [self.viewModel registerObserver:self];
    [self.viewModel loadDetailedMarketPrice];
    [self.viewModel loadDiagramPointsWithPeriod:((NSNumber*)self.timeTypeOptionBar.selectedOption.value).integerValue completionHandler:^(BOOL success) {
        if (success) {
            [self updateTopDiagramViewAnimated:YES];
            [self updateBottomDiagramViewAnimated:YES];
        }
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDiagram) name:KFDDetailedDiagramViewShouldUpdateNotification object:nil];
    
//    AudioServicesPlaySystemSound(1002);
}

-(void)updateDiagram
{
    [self updateTopDiagramViewAnimated:NO];
    [self updateBottomDiagramViewAnimated:NO];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KFDDetailedDiagramViewShouldUpdateNotification object:nil];
}

-(void)defineHeights
{
    _naviBarHeight=self.navigationController.navigationBar.frame.size.height+20;
    _boardHeight=69.5;
    _barHeight=30;
    _topDiagramOptionBarHeight=15;
    _topDiagramViewHeight=SCREEN_HEIGHT*0.4;
    _bottomDiagramOptionBarHeight=15;
    _bottomDiagramViewHeight=MAX(80, SCREEN_HEIGHT-_naviBarHeight-_boardHeight-_barHeight-_topDiagramOptionBarHeight-_topDiagramViewHeight-_bottomDiagramOptionBarHeight-90);
}

-(void)alarmButtonPressed
{
    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
    if (accountManager.isLogin) {
        KFDAlarmSettingsViewController* alarmSettingsVC=[[UIStoryboard storyboardWithName:@"KFDAlarmSettings" bundle:nil] instantiateViewControllerWithIdentifier:@"KFDAlarmSettingsViewController"];
        KFDMarketPrice* marketPrice=self.viewModel.marketPrices[self.viewModel.currentPriceIndex];
        alarmSettingsVC.type=marketPrice.typename;
        [self.navigationController pushViewController:alarmSettingsVC animated:YES];
    }else{
        [UIAlertController presentLoginRegisterAlertControllerWithMessage:@"设置预警前请先登录" byViewController:self];
    }
}

-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"detailedMarketPrice"]) {
        KFDDetailedDiagramViewModel* viewModel=object;
        KFDDetailedMarketPrice* price=viewModel.detailedMarketPrice;
        dispatch_async(dispatch_get_main_queue(), ^{
            self.currentPriceBoard.price=price;
            self.highLowPriceBoard.price=price;
            self.closeOpenPriceBoard.price=price;
        });
    }
}

-(void)setupTitleView
{
    UIView* titleView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    self.titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(20, 0, 60, 30)];
    self.titleLabel.textColor=KFD_WHITE;
    self.titleLabel.textAlignment=NSTextAlignmentCenter;
    self.titleLabel.font=[UIFont systemFontOfSize:15];
    if (self.viewModel.marketPrices.count>0) {
        KFDMarketPrice* marketPrice=self.viewModel.marketPrices[self.viewModel.currentPriceIndex];
        self.titleLabel.text=marketPrice.name;
    }
    [titleView addSubview:self.titleLabel];
    UIButton* leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"arrows-white-left"] forState:UIControlStateNormal];
    leftButton.frame=CGRectMake(0, 5, 20, 20);
    [leftButton addTarget:self action:@selector(leftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:leftButton];
    UIButton* rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"arrows-white-right"] forState:UIControlStateNormal];
    rightButton.frame=CGRectMake(80, 5, 20, 20);
    [rightButton addTarget:self action:@selector(rightButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:rightButton];
    self.navigationItem.titleView=titleView;
}

-(void)leftButtonPressed
{
    self.viewModel.currentPriceIndex-=1;
    if (self.viewModel.currentPriceIndex<0) {
        self.viewModel.currentPriceIndex=self.viewModel.marketPrices.count-1;
    }
    [self.viewModel loadDetailedMarketPrice];
    [self.viewModel loadDiagramPointsWithPeriod:((NSNumber*)self.timeTypeOptionBar.selectedOption.value).integerValue completionHandler:^(BOOL success) {
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                KFDMarketPrice* price=self.viewModel.marketPrices[self.viewModel.currentPriceIndex];
                self.titleLabel.text=price.name;
                [self updateTopDiagramViewAnimated:YES];
                [self updateBottomDiagramViewAnimated:YES];
            });
        }
    }];
}
-(void)rightButtonPressed
{
    self.viewModel.currentPriceIndex+=1;
    if (self.viewModel.currentPriceIndex>self.viewModel.marketPrices.count-1) {
        self.viewModel.currentPriceIndex=0;
    }
    [self.viewModel loadDetailedMarketPrice];
    [self.viewModel loadDiagramPointsWithPeriod:((NSNumber*)self.timeTypeOptionBar.selectedOption.value).integerValue completionHandler:^(BOOL success) {
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                KFDMarketPrice* price=self.viewModel.marketPrices[self.viewModel.currentPriceIndex];
                self.titleLabel.text=price.name;
                [self updateTopDiagramViewAnimated:YES];
                [self updateBottomDiagramViewAnimated:YES];
            });
        }
    }];
}

//-----------------------------------------------------------------------
// Setup diagram
//-----------------------------------------------------------------------
-(void)setupDiagramTheme
{
    BBTheme* theme=[BBTheme theme];
    theme.backgroundColor=KFD_DIAGRAM_BG;
    theme.xAxisFontSize = 8;
    theme.yAxisFontSize = 8;
    theme.riseColor=KFD_UP_COLOR;
    theme.fallColor=KFD_DOWN_COLOR;
    theme.barBorderColorNegative=KFD_GREEN;
    theme.barFillColorNegative=KFD_GREEN;
    theme.barBorderColorPositive=KFD_RED;
    theme.barFillColorPositive=KFD_RED;
    theme.axisColor=KFD_TEXT_DARK_GRAY;
}
-(void)setupDiagramContainer
{
    self.diagramContainer=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 190, SCREEN_WIDTH, SCREEN_HEIGHT-240)];
    self.diagramContainer.backgroundColor=KFD_DIAGRAM_BG;
    [self.view addSubview:self.diagramContainer];
    self.diagramContainer.contentSize=CGSizeMake(SCREEN_WIDTH, _topDiagramOptionBarHeight+2+_topDiagramViewHeight+8+_bottomDiagramOptionBarHeight+_bottomDiagramViewHeight);
//    self.diagramContainer.showsVerticalScrollIndicator=NO;
    UIPanGestureRecognizer* panRecognizer=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(diagramPanned:)];
    panRecognizer.delegate=self;
    [self.diagramContainer addGestureRecognizer:panRecognizer];
    self.diagramContainer.showsVerticalScrollIndicator=NO;
}
-(void)setupTopDiagramOptionBar
{
    self.topDiagramOptionBar=[[JTOptionBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/3, _topDiagramOptionBarHeight)];
    JTOptionBarConfiguration* config=[JTOptionBarConfiguration config];
    config.font=[UIFont systemFontOfSize:8];
    config.backgroundColor=KFD_WHITE;
    config.indicatorImage=[UIImage imageNamed:@"arrowstyle-up-reg"];
    self.topDiagramOptionBar.config=config;
    __weak typeof(self) weakSelf=self;
    [self.topDiagramOptionBar addOptionWithValue:@"ma" title:@"MA" action:^(JTOptionBar *optionBar) {
        weakSelf.maPriceLabel.hidden=NO;
        weakSelf.bollPriceLabel.hidden=YES;
        [weakSelf updateTopDiagramViewAnimated:YES];
    }];
    [self.topDiagramOptionBar addOptionWithValue:@"boll" title:@"BOLL" action:^(JTOptionBar *optionBar) {
        weakSelf.maPriceLabel.hidden=YES;
        weakSelf.bollPriceLabel.hidden=NO;
        [weakSelf updateTopDiagramViewAnimated:YES];
    }];
    [self.diagramContainer addSubview:self.topDiagramOptionBar];
}
-(void)setupTopDiagramView
{
    self.topDiagramView=[[BBChartView alloc] initWithFrame:CGRectMake(-10, 24, SCREEN_WIDTH+5, _topDiagramViewHeight)];
    [self.diagramContainer addSubview:self.topDiagramView];
}
-(void)setupTopPriceLabel
{
    self.maPriceLabel=[[NSBundle mainBundle] loadNibNamed:@"KFDMAPriceLabel" owner:nil options:nil].firstObject;
    self.maPriceLabel.frame=CGRectMake(4, 16, SCREEN_WIDTH, 10);
    [self.diagramContainer addSubview:self.maPriceLabel];
    self.bollPriceLabel=[[NSBundle mainBundle] loadNibNamed:@"KFDBOLLPriceLabel" owner:nil options:nil].firstObject;
    self.bollPriceLabel.frame=CGRectMake(4, 16, SCREEN_WIDTH, 10);
    [self.diagramContainer addSubview:self.bollPriceLabel];
    self.bollPriceLabel.hidden=YES;
}
-(void)setupBottomDiagramOptionBar
{
    self.bottomDiagramOptionBar=[[JTOptionBar alloc] initWithFrame:CGRectMake(0, _topDiagramOptionBarHeight+5+_topDiagramViewHeight-10, SCREEN_WIDTH/3, _bottomDiagramOptionBarHeight)];
    JTOptionBarConfiguration* config=[JTOptionBarConfiguration config];
    config.backgroundColor=KFD_WHITE;
    config.font=[UIFont systemFontOfSize:8];
    config.indicatorImage=[UIImage imageNamed:@"arrowstyle-up-reg.png"];
    self.bottomDiagramOptionBar.config=config;
    __weak typeof(self) weakSelf=self;
    [self.bottomDiagramOptionBar addOptionWithValue:@"macd" title:@"MACD" action:^(JTOptionBar *optionBar) {
        [weakSelf updateBottomDiagramViewAnimated:YES];
        weakSelf.macdPriceLabel.hidden=NO;
        weakSelf.kdjPriceLabel.hidden=YES;
    }];
    [self.bottomDiagramOptionBar addOptionWithValue:@"kdj" title:@"KDJ" action:^(JTOptionBar *optionBar) {
        [weakSelf updateBottomDiagramViewAnimated:YES];
        weakSelf.macdPriceLabel.hidden=YES;
        weakSelf.kdjPriceLabel.hidden=NO;
    }];
    [self.diagramContainer addSubview:self.bottomDiagramOptionBar];
}
-(void)setupBottomPriceLabel
{
    self.macdPriceLabel=[[NSBundle mainBundle] loadNibNamed:@"KFDMACDPriceLabel" owner:nil options:nil].firstObject;
    self.macdPriceLabel.frame=CGRectMake(4, _topDiagramOptionBarHeight+5+_topDiagramViewHeight+6, SCREEN_WIDTH, 10);
    [self.diagramContainer addSubview:self.macdPriceLabel];
    
    self.kdjPriceLabel=[[NSBundle mainBundle] loadNibNamed:@"KFDKDJPriceLabel" owner:nil options:nil].firstObject;
    self.kdjPriceLabel.frame=CGRectMake(4, _topDiagramOptionBarHeight+5+_topDiagramViewHeight+6, SCREEN_WIDTH, 10);
    [self.diagramContainer addSubview:self.kdjPriceLabel];
    self.kdjPriceLabel.hidden=YES;
}
-(void)setupBottomDiagramView
{
    self.bottomDiagramView=[[BBChartView alloc] initWithFrame:CGRectMake(-10, _bottomDiagramOptionBarHeight+5+_topDiagramViewHeight+10+_bottomDiagramOptionBarHeight-5, SCREEN_WIDTH+5, _bottomDiagramViewHeight)];
    [self.diagramContainer addSubview:self.bottomDiagramView];
}
//-----------------------------------------------------------------------

-(void)diagramPanned:(UIPanGestureRecognizer*)recognizer
{
    CGPoint translation=[recognizer translationInView:recognizer.view];
    if (translation.x==0||translation.y/translation.x>1|translation.y/translation.x<-1) {
        
    }else if(translation.x>0){
        int count=translation.x/100;
        for (int i=0; i<count; i++) {
            if ([self.viewModel loadLeftPointOfPeriodType:((NSNumber*)self.timeTypeOptionBar.selectedOption.value).integerValue]) {
                break;
            }
        }
    }else{
        int count=-translation.x/100;
        KFDDiagramPoints* dp;
        switch (((NSNumber*)self.timeTypeOptionBar.selectedOption.value).integerValue) {
            case KFDDiagramDataPeriod5Minutes:
                dp=self.viewModel.diagramPoints5Minutes;
                break;
            case KFDDiagramDataPeriod15Minutes:
                dp=self.viewModel.diagramPoints15Minutes;
                break;
            case KFDDiagramDataPeriod30Minutes:
                dp=self.viewModel.diagramPoints30Minutes;
                break;
            case KFDDiagramDataPeriod1Hour:
                dp=self.viewModel.diagramPoints1Hour;
            default:
                break;
        }
        for (int i=0; i<count; i++) {
            if (dp.leftPointShown+dp.numberOfPointsShown<dp.diagramPoints.count) {
                dp.leftPointShown+=1;
            }
        }
        [self updateDiagram];
    }
}

//-----------------------------------------------------------------------
// Update diagram
//-----------------------------------------------------------------------
-(void)updateTopDiagramViewAnimated:(BOOL)animated
{
    dispatch_async(dispatch_get_main_queue(), ^{
        Area* area0=[Area new];
        area0.bottomAxis.labelProvider=self;
        
        [self.topDiagramView removeAllAreas];
        [self.topDiagramView addArea:area0];
        
        StockSeries* stockSeries=[StockSeries new];
        [area0 addSeries:stockSeries];
        
        LineSeries* lineSeries0=[LineSeries new];
        lineSeries0.color=KFD_K_MID_COLOR;
        [area0 addSeries:lineSeries0];
        
        LineSeries* lineSeries1=[LineSeries new];
        lineSeries1.color=KFD_K_UP_COLOR;
        [area0 addSeries:lineSeries1];
        
        LineSeries* lineSeries2=[LineSeries new];
        lineSeries2.color=KFD_K_DOWN_COLOR;
        [area0 addSeries:lineSeries2];
        NSArray* diagramPointsShown=nil;
        @try {
            KFDDiagramPoints* dp=[self getDiagramPointsOfSelectedPeriodType];
            diagramPointsShown=[dp.diagramPoints subarrayWithRange:NSMakeRange(dp.leftPointShown, dp.numberOfPointsShown)];
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
        }
        for (KFDDiagramPoint* diagramPoint in diagramPointsShown) {
            [stockSeries addPointOpen:Float(diagramPoint.open_price) close:Float(diagramPoint.close_price) low:Float(diagramPoint.low_price) high:Float(diagramPoint.high_price)];
            if ([self.topDiagramOptionBar.selectedOption.value isEqualToString:@"ma"]) {
                [lineSeries0 addPoint:Float(diagramPoint.ma[0])];
                [lineSeries1 addPoint:Float(diagramPoint.ma[1])];
                [lineSeries2 addPoint:Float(diagramPoint.ma[2])];
            }else if([self.topDiagramOptionBar.selectedOption.value isEqualToString:@"boll"]){
                [lineSeries0 addPoint:Float(diagramPoint.boll[0])];
                [lineSeries1 addPoint:Float(diagramPoint.boll[1])];
                [lineSeries2 addPoint:Float(diagramPoint.boll[2])];
            }
        }
        if ([self.topDiagramOptionBar.selectedOption.value isEqualToString:@"ma"]) {
            self.maPriceLabel.diagramPoint=diagramPointsShown.lastObject;
        }else if([self.topDiagramOptionBar.selectedOption.value isEqualToString:@"boll"]){
            self.bollPriceLabel.diagramPoint=diagramPointsShown.lastObject;
        }
        [self.topDiagramView drawAnimated:animated];
    });
}
-(void)updateBottomDiagramViewAnimated:(BOOL)animated
{
    dispatch_async(dispatch_get_main_queue(), ^{
        Area* area1=[Area new];
        area1.bottomAxis.labelProvider=self;
        [self.bottomDiagramView addArea:area1];
        [self.bottomDiagramView removeAllAreas];
        [self.bottomDiagramView addArea:area1];
        
        KFDDiagramPoints* dp=[self getDiagramPointsOfSelectedPeriodType];
        NSArray* diagramPointsShown=nil;
        @try {
            diagramPointsShown=[dp.diagramPoints subarrayWithRange:NSMakeRange(dp.leftPointShown, dp.numberOfPointsShown)];
        }
        @catch (NSException *exception) {
            [self.viewModel loadDiagramPointsWithPeriod:((NSNumber*)self.timeTypeOptionBar.selectedOption.value).integerValue completionHandler:nil];
        }
        @finally {
            
        }
        
        if ([self.bottomDiagramOptionBar.selectedOption.value isEqualToString:@"macd"]) {
            BarSeries* barSeries=[BarSeries new];
            [area1 addSeries:barSeries];
//            [BBTheme theme].barFillColor=KFD_GREEN;
            
            LineSeries* deaSeries=[LineSeries new];
            deaSeries.color=KFD_K_UP_COLOR;
            [area1 addSeries:deaSeries];
            
            LineSeries* difSeries=[LineSeries new];
            difSeries.color=KFD_K_DOWN_COLOR;
            [area1 addSeries:difSeries];
            
            for (KFDDiagramPoint* diagramPoint in diagramPointsShown) {
                [deaSeries addPoint:Float(diagramPoint.macd[0])];
                [difSeries addPoint:Float(diagramPoint.macd[1])];
                [barSeries addPoint:Float(diagramPoint.macd[2])];
            }
            self.macdPriceLabel.diagramPoint=diagramPointsShown.lastObject;
        }else if ([self.bottomDiagramOptionBar.selectedOption.value isEqualToString:@"kdj"]){
            LineSeries* kSeries=[LineSeries new];
            kSeries.color=KFD_K_MID_COLOR;
            [area1 addSeries:kSeries];
            
            LineSeries* dSeries=[LineSeries new];
            dSeries.color=KFD_K_UP_COLOR;
            [area1 addSeries:dSeries];
            
            LineSeries* jSeries=[LineSeries new];
            jSeries.color=KFD_K_DOWN_COLOR;
            [area1 addSeries:jSeries];
            
            for (KFDDiagramPoint* diagramPoint in diagramPointsShown) {
                [kSeries addPoint:Float(diagramPoint.kdj[0])];
                [dSeries addPoint:Float(diagramPoint.kdj[1])];
                [jSeries addPoint:Float(diagramPoint.kdj[2])];
            }
            self.kdjPriceLabel.diagramPoint=diagramPointsShown.lastObject;
        }
        
        [self.bottomDiagramView drawAnimated:animated];
    });
}
//-----------------------------------------------------------------------

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setupPriceBoards
{
    CGFloat boardWidth=(SCREEN_WIDTH-1)/3;
    NSBundle* bundle=[NSBundle mainBundle];
    
    self.currentPriceBoard=[bundle loadNibNamed:@"KFDCurrentPriceBoard" owner:nil options:nil].firstObject;
    self.currentPriceBoard.frame=CGRectMake(0, _naviBarHeight+7.5, boardWidth, _boardHeight);
    [self.view addSubview:self.currentPriceBoard];
    
    self.highLowPriceBoard=[bundle loadNibNamed:@"KFDHighLowPriceBoard" owner:nil options:nil].firstObject;
    self.highLowPriceBoard.frame=CGRectMake(boardWidth+0.5, _naviBarHeight+7.5, boardWidth, _boardHeight);
    [self.view addSubview:self.highLowPriceBoard];
    
    self.closeOpenPriceBoard=[bundle loadNibNamed:@"KFDCloseOpenPriceBoard" owner:nil options:nil].firstObject;
    self.closeOpenPriceBoard.frame=CGRectMake(boardWidth*2+1, _naviBarHeight+7.5, boardWidth, _boardHeight);
    [self.view addSubview:self.closeOpenPriceBoard];
}

-(void)setupTimeTypeOptionBar
{
    self.timeTypeOptionBar=[[JTOptionBar alloc] initWithFrame:CGRectMake(0, _naviBarHeight+7.5+_boardHeight+10, SCREEN_WIDTH, _barHeight)];
    JTOptionBarConfiguration* config=[JTOptionBarConfiguration config];
    config.backgroundColor=KFD_WHITE;
    config.indicatorImage=[UIImage imageNamed:@"arrowstyle-up-reg.png"];
    self.timeTypeOptionBar.config=config;
    __weak typeof(self) weakSelf=self;
    [self.timeTypeOptionBar addOptionWithValue:@(KFDDiagramDataPeriod5Minutes) title:@"5分钟" action:^(JTOptionBar *optionBar) {
        [weakSelf.viewModel loadDiagramPointsWithPeriod:KFDDiagramDataPeriod5Minutes completionHandler:^(BOOL success) {
            [weakSelf updateTopDiagramViewAnimated:YES];
            [weakSelf updateBottomDiagramViewAnimated:YES];
        }];
    }];
    [self.timeTypeOptionBar addOptionWithValue:@(KFDDiagramDataPeriod15Minutes) title:@"15分钟" action:^(JTOptionBar *optionBar) {
        [weakSelf.viewModel loadDiagramPointsWithPeriod:KFDDiagramDataPeriod15Minutes completionHandler:^(BOOL success) {
            [weakSelf updateTopDiagramViewAnimated:YES];
            [weakSelf updateBottomDiagramViewAnimated:YES];
        }];
    }];
    [self.timeTypeOptionBar addOptionWithValue:@(KFDDiagramDataPeriod30Minutes) title:@"30分钟" action:^(JTOptionBar *optionBar) {
        [weakSelf.viewModel loadDiagramPointsWithPeriod:KFDDiagramDataPeriod30Minutes completionHandler:^(BOOL success) {
            [weakSelf updateTopDiagramViewAnimated:YES];
            [weakSelf updateBottomDiagramViewAnimated:YES];
        }];
    }];
    [self.timeTypeOptionBar addOptionWithValue:@(KFDDiagramDataPeriod1Hour) title:@"1小时" action:^(JTOptionBar *optionBar) {
        [weakSelf.viewModel loadDiagramPointsWithPeriod:KFDDiagramDataPeriod1Hour completionHandler:^(BOOL success) {
            [weakSelf updateTopDiagramViewAnimated:YES];
            [weakSelf updateBottomDiagramViewAnimated:YES];
        }];
    }];
    [self.view addSubview:self.timeTypeOptionBar];
}

-(KFDDiagramPoints*)getDiagramPointsOfSelectedPeriodType
{
    switch (((NSNumber*)self.timeTypeOptionBar.selectedOption.value).integerValue) {
        case KFDDiagramDataPeriod5Minutes:
            return self.viewModel.diagramPoints5Minutes;
            break;
        case KFDDiagramDataPeriod15Minutes:
            return self.viewModel.diagramPoints15Minutes;
            break;
        case KFDDiagramDataPeriod30Minutes:
            return self.viewModel.diagramPoints30Minutes;
            break;
        case KFDDiagramDataPeriod1Hour:
            return self.viewModel.diagramPoints1Hour;
        default:
            return nil;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
//    [self.viewModel reset];
}

//-----------------------------------------------------------------------
#pragma mark - AxisXLabelProvider
//-----------------------------------------------------------------------
-(NSString*)textForIdx:(NSUInteger)idx
{
    KFDDiagramPoints* dp=[self getDiagramPointsOfSelectedPeriodType];
    NSInteger modal=dp.numberOfPointsShown/4;
    if (idx%modal==0) {
        KFDDiagramPoint* diagramPoint=dp.diagramPoints[dp.leftPointShown+idx];
        return [NSString stringWithFormat:@"%@ %@",diagramPoint.date.month_day,diagramPoint.date.hour_minute];
    }
    return @"";
}
//-----------------------------------------------------------------------

//-----------------------------------------------------------------------
#pragma mark - Gesture recognizer delegate
//-----------------------------------------------------------------------
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
//-----------------------------------------------------------------------

@end
